package org.creavi.engine.hud;

import java.nio.FloatBuffer;

import com.jme.math.Vector3f;

public class GaugeHudInstance extends HudInstance {

	private GaugeType type;
	
	private boolean stretch;
	
	private float min;
	
	private float max;
	
	private float value;
	
	public GaugeHudInstance(String name, HudDefinition definition) {
		super(name, definition);
		type = GaugeType.Left;
		stretch = true;
		min = 0;
		max = 100;
		value = 100;
	}
	
	public boolean isStretch() {
		return stretch;
	}


	public void setStretch(boolean stretch) {
		this.stretch = stretch;
	}

	public GaugeType getType() {
		return type;
	}

	public void setType(GaugeType type) {
		this.type = type;
	}
	
	public float getMax() {
		return max;
	}

	public void setMax(float max) {
		this.max = max;
	}

	public float getMin() {
		return min;
	}

	public void setMin(float min) {
		this.min = min;
	}

	public float getValue() {
		return value;
	}

	public void setValue(float value) {
		this.value = value;
	}

	public void update() {
		float width = getDefinition().getWidth();
		float height = getDefinition().getHeight();
		
		if (type == GaugeType.Left) {
			float porc = value - min;
			porc = value / (max - min);
			
			getHudQuad().setLocalScale(new Vector3f(porc, 1, 1));
			getHudQuad().setLocalTranslation(getHudNode().getLocalTranslation().add((width / 2) - ((width / 2) * (1 - porc)), height / 2, 0));

			if (!stretch) {
				FloatBuffer fb = getHudQuad().getTextureCoords().get(0).coords;
				
				float xuv = (getDefinition().getUv2().x - getDefinition().getUv1().x) * porc;
				xuv += getDefinition().getUv1().x;
				
				fb.put(4, xuv);
				fb.put(6, xuv);
			}
		} else if (type == GaugeType.Right) {
			float porc = value - min;
			porc = value / (max - min);
			
			getHudQuad().setLocalScale(new Vector3f(porc, 1, 1));
			getHudQuad().setLocalTranslation(getHudNode().getLocalTranslation().add((width / 2) + ((width / 2) * (1 - porc)), height / 2, 0));
			if (!stretch) {
				FloatBuffer fb = getHudQuad().getTextureCoords().get(0).coords;
				
				float xuv = (getDefinition().getUv2().x - getDefinition().getUv1().x) * porc;
				xuv = getDefinition().getUv2().x - xuv;
				
				fb.put(0, xuv);
				fb.put(2, xuv);
			}
		} else if (type == GaugeType.Up) {
			float porc = value - min;
			porc = value / (max - min);
			
			getHudQuad().setLocalScale(new Vector3f(1, porc, 1));
			getHudQuad().setLocalTranslation(getHudNode().getLocalTranslation().add(width / 2, (height / 2) + ((height / 2) * (1 - porc)), 0));
			if (!stretch) {
				FloatBuffer fb = getHudQuad().getTextureCoords().get(0).coords;
				
				float yuv = (getDefinition().getUv1().y - getDefinition().getUv2().y) * porc;
				yuv = getDefinition().getUv1().y - yuv;
				
				fb.put(3, yuv);
				fb.put(5, yuv);
			}
		} else if (type == GaugeType.Down) {
			float porc = value - min;
			porc = value / (max - min);
			
			getHudQuad().setLocalScale(new Vector3f(1, porc, 1));
			getHudQuad().setLocalTranslation(getHudNode().getLocalTranslation().add(width / 2, (height / 2) - ((height / 2) * (1 - porc)), 0));
			if (!stretch) {
				FloatBuffer fb = getHudQuad().getTextureCoords().get(0).coords;
				
				float yuv = (getDefinition().getUv1().y - getDefinition().getUv2().y) * porc;
				yuv = getDefinition().getUv2().y + yuv;
				
				fb.put(1, yuv);
				fb.put(7, yuv);
			}
		} else if (type == GaugeType.HorizontalCenter) {
			float porc = value - min;
			porc = value / (max - min);
			
			getHudQuad().setLocalScale(new Vector3f(porc, 1, 1));
			if (!stretch) {
				FloatBuffer fb = getHudQuad().getTextureCoords().get(0).coords;
				
				float duv = (getDefinition().getUv2().x - getDefinition().getUv1().x) * (1 - porc);
				float x1 = getDefinition().getUv1().x + (duv / 2);
				float x2 = getDefinition().getUv2().x - (duv / 2);
				
				fb.put(0, x1);
				fb.put(2, x1);
				fb.put(4, x2);
				fb.put(6, x2);
			}
		} else if (type == GaugeType.VerticalCenter) {
			float porc = value - min;
			porc = value / (max - min);
			
			getHudQuad().setLocalScale(new Vector3f(1, porc, 1));
			if (!stretch) {
				FloatBuffer fb = getHudQuad().getTextureCoords().get(0).coords;
				
				float duv = (getDefinition().getUv1().y - getDefinition().getUv2().y) * (1 - porc);
				float y1 = getDefinition().getUv1().y - (duv / 2);
				float y2 = getDefinition().getUv2().y + (duv / 2);
				
				fb.put(1, y1);
				fb.put(7, y1);
				fb.put(3, y2);
				fb.put(5, y2);
			}
		}		
	}
}
